using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class SupplierQualificationsController : ControllerBase
    {
        private readonly ISupplierQualificationService _qualificationService;

        public SupplierQualificationsController(ISupplierQualificationService qualificationService)
        {
            _qualificationService = qualificationService;
        }

        // GET: api/SupplierQualifications/supplier/5
        [HttpGet("supplier/{supplierId}")]
        public async Task<ActionResult<IEnumerable<SupplierQualification>>> GetQualificationsBySupplierId(int supplierId)
        {
            var qualifications = await _qualificationService.GetQualificationsBySupplierIdAsync(supplierId);
            return Ok(qualifications);
        }

        // GET: api/SupplierQualifications/5
        [HttpGet("{id}")]
        public async Task<ActionResult<SupplierQualification>> GetQualificationById(int id)
        {
            var qualification = await _qualificationService.GetQualificationByIdAsync(id);

            if (qualification == null)
            {
                return NotFound();
            }

            return qualification;
        }

        // POST: api/SupplierQualifications
        [HttpPost]
        public async Task<ActionResult<SupplierQualification>> PostQualification(SupplierQualification qualification)
        {
            try
            {
                var createdQualification = await _qualificationService.CreateQualificationAsync(qualification);
                return CreatedAtAction("GetQualificationById", new { id = createdQualification.Id }, createdQualification);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // PUT: api/SupplierQualifications/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutQualification(int id, SupplierQualification qualification)
        {
            if (id != qualification.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _qualificationService.UpdateQualificationAsync(qualification);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // DELETE: api/SupplierQualifications/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteQualification(int id)
        {
            var deleted = await _qualificationService.DeleteQualificationAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }

        // PATCH: api/SupplierQualifications/5/status
        [HttpPatch("{id}/status")]
        public async Task<IActionResult> ChangeQualificationStatus(int id, [FromBody] byte status)
        {
            var success = await _qualificationService.ChangeQualificationStatusAsync(id, status);
            if (!success)
            {
                return NotFound();
            }

            return NoContent();
        }
    }
}